home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / CreateObject.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  4.4 KB  |  209 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CreateObject.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef    __CREATEOBJECT__
  15. #include "CreateObject.h"
  16. #endif
  17.  
  18. #ifndef    __STDIO__
  19. #include "StdIO.h"
  20. #endif
  21.  
  22. #ifndef    __STRING__
  23. #include "String.h"
  24. #endif
  25.  
  26. #ifndef    __MEMORY__
  27. #include "Memory.h"
  28. #endif
  29.  
  30. #ifndef    __RESOURCES__
  31. #include "Resources.h"
  32. #endif
  33.  
  34. #ifndef    __DEBUGASSERT__
  35. #include "DebugAssert.h"
  36. #endif
  37.  
  38. #ifndef    __DEBUGGINGGEAR__
  39. #include "DebuggingGear.h"
  40. #endif
  41.  
  42. #pragma segment CreateObject
  43.  
  44. /***********************************|****************************************/
  45.  
  46. const OSType TDefaultConstructorIterator::kCodeResourceType = 'CODE';
  47. const OSType TDefaultConstructorIterator::kLookupResourceType = 'FcAd';
  48. const short TDefaultConstructorIterator::kLookupResourceID = 128;
  49.  
  50. /***********************************|****************************************/
  51.  
  52. TDefaultConstructorIterator::TDefaultConstructorIterator ():
  53.     fResource ( nil ),
  54.     fState ( 0 ),
  55.     fMaxName ( nil ),
  56.     fCurrentName ( nil ),
  57.     fID ( -1 ),
  58.     fOffset ( 0 )
  59. {
  60.     SetupLookupResource ( GetLookupResource () );
  61. }
  62.  
  63. /***********************************|****************************************/
  64.  
  65. TDefaultConstructorIterator::TDefaultConstructorIterator ( const Handle data ):
  66.     fResource ( nil ),
  67.     fState ( 0 ),
  68.     fMaxName ( nil ),
  69.     fCurrentName ( nil ),
  70.     fID ( -1 ),
  71.     fOffset ( 0 )
  72. {
  73.     SetupLookupResource ( (Handle) data );
  74. }
  75.  
  76. /***********************************|****************************************/
  77.  
  78. TDefaultConstructorIterator::~TDefaultConstructorIterator ()
  79. {
  80.     if ( fResource )
  81.         ::HSetState ( fResource, fState );
  82. }
  83.  
  84. /***********************************|****************************************/
  85.  
  86. void
  87. TDefaultConstructorIterator::SetupLookupResource ( Handle data )
  88. {
  89.     if ( data )
  90.     {
  91.         fResource = data;
  92.         fState = ::HGetState ( fResource );
  93.         ::HLock ( fResource );
  94.         fCurrentName = *fResource;
  95.         fMaxName = fCurrentName + ::GetHandleSize ( fResource );
  96.         fID = -1;
  97.         fOffset = 0;
  98.     }
  99. #if debug
  100.     else
  101.     {
  102.         chris << "\n### The ‘FcAd’ could not be found in your application\n";
  103.         chris << "### Please run the MakeFuncAddrRes tool on the app linkmap,\n";
  104.         chris << "### then Rez the output file into the app after linking.\n\n";
  105.     }
  106. #endif
  107. }
  108.  
  109. /***********************************|****************************************/
  110.  
  111. const char*
  112. TDefaultConstructorIterator::FirstConstructor ()
  113. {
  114.     fCurrentName = *fResource;
  115.     return NextConstructor ();
  116. }
  117.  
  118. /***********************************|****************************************/
  119.  
  120. const char*
  121. TDefaultConstructorIterator::NextConstructor ()
  122. {
  123.     if ( fCurrentName < fMaxName )
  124.     {
  125.         char* name = fCurrentName;
  126.         
  127.         while ( *fCurrentName++ )  ;
  128.         
  129.         fID = *( (short*) fCurrentName );
  130.         fCurrentName += sizeof ( fID );
  131.         fOffset = *(unsigned long*) fCurrentName;
  132.         fCurrentName += sizeof ( fOffset );
  133.         
  134.         return name;
  135.     }
  136.     else
  137.     {
  138.         return nil;
  139.     }
  140. }
  141.  
  142. /***********************************|****************************************/
  143.  
  144. pascal void LoadSegment ( short ) = 0xA9F0;
  145.  
  146. const Constructor
  147. TDefaultConstructorIterator::GetConstructorAddress () const
  148. {
  149.     if ( fID > 0 )
  150.     {
  151. //         LoadSegment ( fID );
  152.         Handle code = ::Get1Resource ( kCodeResourceType, fID );
  153.         
  154.         if ( code )
  155.         {
  156.             ::HLock ( code );
  157.             return (Constructor) ( *code + fOffset );
  158.         }
  159.         else
  160.             return nil;
  161.     }
  162.     else
  163.         return nil;
  164. }
  165.  
  166. /***********************************|****************************************/
  167.  
  168. Handle
  169. TDefaultConstructorIterator::GetLookupResource ()
  170. {
  171.     // we probably should make sure the current resource file 
  172.     // is the application resource file
  173.     
  174.     return ::Get1Resource ( kLookupResourceType, kLookupResourceID );
  175. }
  176.  
  177. /***********************************|****************************************/
  178.  
  179. static void*
  180. GetConstructorAddress ( const char* className )
  181. {
  182.     TDefaultConstructorIterator iterator;
  183.     
  184.     for ( const char* currentName = iterator.FirstConstructor ();
  185.           currentName != nil;
  186.           currentName = iterator.NextConstructor () )
  187.     {
  188.         if ( ::strcmp ( currentName, className ) == 0 )
  189.             return iterator.GetConstructorAddress ();
  190.     }
  191.     
  192.     return nil;
  193. }
  194.  
  195. /***********************************|****************************************/
  196.  
  197. void*
  198. CreateObject ( const char* className )
  199. {
  200.     Constructor constructor = (Constructor) ::GetConstructorAddress ( className );
  201.     
  202.     if ( constructor )
  203.         return constructor ( nil );
  204.     else
  205.         return nil;
  206. }
  207.  
  208. /***********************************|****************************************/
  209.